//
//  MyBranchViewController.m
//  ShangHaiHuDongApp
//
//  Created by 刘应平 on 2017/6/13.
//  Copyright © 2017年 刘应平. All rights reserved.
//
#import "MyBranchViewController.h"
#import "MyBranchMenuModel.h"
#import "ActivityJoinDetailModel.h"
#import "HomeMenuTableViewCell.h"
#import "MyBranchMenuWithFourItemView.h"
#import "MyBranchThreeTableViewCell.h"
#import "MyBranchShowScoreView.h"
#import "YBRequestManager.h"
#import "StudyTabBarController.h"
#import "ZJParticipateActivitiesViewController.h"
#import "ZJQuestionWebViewController.h"

/**  二维码扫描  */
#import "QRCodeReaderViewController.h"
/**  足迹  */
#import "MyBranchPartyFootPrintViewController.h"
/** 今日签到 */
#import "ZJTodaySingWebViewController.h"

/**  新建活动  */
#import "MyBraCreateNewActViewController.h"
/**  交党费  */
#import "HandPartyMoneyViewController.h"


/**  党员报道  */
#import "ZJReportViewController.h"

#import "ZJWebViewController.h"
#import "ZJPushInfoManager.h"
//cell model
#import "BranchNormalModel.h"

//活动详情界面
#import "LPActivityDetailViewController.h"
#import "ActivityJoinDetailModel.h"

//党建动态详情界面
#import "LPWKHasCommentAndHeadLineViewController.h"
#import "LPDCDetailModel.h"
//需求认领详情界面
#import "ZJShareClaimViewController.h"
#import "ShareOpenRequestModel.h"

/** 消息列表 */
#import "LPMessageListViewController.h"
#import "ZJWebViewController.h"
#import "JPUSHService.h"
#import "LPSignSuccessShowView.h"
#import "LPChooseReportTypeViewController.h"
#define MENUCELL @"menuCell"
#define ACTLISTCELL @"actListCell"

//组织生活cell
#import "OrganizationalLifeTableViewCell.h"
#define ORGCELL @"OrganizationalLifeTableViewCell"

//需求认领cell
#import "NeedClaimTableViewCell.h"
#define NEEDCELL @"NeedClaimTableViewCell"

//党费交纳cell
#import "YBDuesPayTableViewCell.h"
#define DUESCELL @"YBDuesPayTableViewCell"

//党建动态cell
#import "ConstructionDynamicTableViewCell.h"
#define CONSTRCELL @"ConstructionDynamicTableViewCell"
//首页广告图cell
#import "YBHomeLogoTableViewCell.h"
#define HOMELOGOCELL @"YBHomeLogoTableViewCell"
//组织生活model
#import "BranchActivityModel.h"
//需求认领model
#import "BranchRequirementModel.h"
//党费交纳model
#import "YBBranchNoticeModel.h"
//党建动态model
#import "YBBranchNewsModel.h"
#import "ZJIntroductionView.h"
/**  中间菜单栏  */
#import "TopImageAndLabelModel.h"
/**  线上组织生活  */
#import "OnlineOrganizationalLifeViewController.h"
#import "ZJDiscussViewController.h"
#import "OnlineOLDetailViewController.h"
#import "OnlineOrganizationalLifeModel.h"
#import "YBQuestionnaireListTableViewCell.h"
/**  网上党校  */
#import "LPNineTenwtySixOnlineSchoolViewController.h"
/**  支部动态  */
#import "LPNineTwentySixPartyDynamicViewController.h"
/**  常见问题  */
#import "LPNineTwentyNormalQuestionViewController.h"
#import "LPNewCommonActListViewController.h"

@interface MyBranchViewController ()<UITableViewDataSource,UITableViewDelegate,MyBranchMenuWithFourItemDelegate,LPCostomHeadLineAndTextViewAndTiShiDelegate>
{
    NSInteger _ORG_LIFE;/**< 标记 组织生活 的数量 */
    NSInteger _STUDY;/**< 标记 学习教育 的数量 */
    NSInteger _GY;/**< 标记 公益活动 的数量 */
    NSInteger _NEWS_TOTAL;/**< 标记 支部动态 的数量 */
    NSString *_JF;/**< 标记 积分 的数量 */
}
/**  table  */
@property (strong, nonatomic) UITableView  *tableView;

/**  第一个菜单数组  */
@property (strong, nonatomic) NSArray  *firstTitleAndImages;
/**  第一个菜单视图  */
@property (strong, nonatomic) MyBranchMenuWithFourItemView  *firstMenuView;

/**  数据  */
@property (strong, nonatomic) NSMutableArray  *dataSouce;

/**  页数  */
@property (assign, nonatomic) NSInteger  page;
/** 滑动 */
@property (nonatomic, strong) ZJRefurbish *refurbish;

//cell高度数组
@property (nonatomic , strong) NSMutableArray * cellHeightArray;
//cell model数组
@property (nonatomic , strong) NSMutableArray * modelArray;
//标题高度数组
@property (nonatomic , strong) NSMutableArray * titleHeightArray;
//cell title起始高度
@property (nonatomic , assign) CGFloat cellBeginY;
//一个16号字的高度
@property (nonatomic , assign) CGFloat itemTextHeight;
//是否第一次进入
@property (nonatomic , assign) BOOL isFirstIn;

/**  jc  */
@property (strong, nonatomic) JCAlertView  *alertView;

/**  当前选中活动model  */
@property (strong, nonatomic) BranchActivityModel  *currentSelectModel;

/**  当前选中需求认领model  */
@property (strong, nonatomic) BranchRequirementModel  *currentSelectRequireModel;
/** 菜单视图    */
@property (nonatomic, strong) MyBranchMenuWithFourItemView *threeMenuView;
/**  菜单数组  */
@property (strong, nonatomic) NSArray  *threeTitlesAndImage;
@end

@implementation MyBranchViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //查询未读消息数量
    [self changeNotiBadge];
    
    [self netWorking];
}


//修改红色角标数量
-(void)changeNotiBadge{
    NSInteger unReadNumber = [[ZJPushInfoManager sharedPushInfo] unReadMessageNumber];
    [self.firstMenuView setMessageNumberWithNumber:unReadNumber];

    [UIApplication sharedApplication].applicationIconBadgeNumber  = unReadNumber;
    [JPUSHService setBadge:unReadNumber];
}


-(void)option_receiveNotificationWithNoti:(NSNotification *)noti{
    [self changeNotiBadge];
}


- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    /**  签到小红点  */
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"YBIsSign"] integerValue] == 1) {
        [self.firstMenuView setRedViewHideOrShow:YES];
    }
    else{
        [self.firstMenuView setRedViewHideOrShow:NO];
    }

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _JF = @"0";
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(option_receiveNotificationWithNoti:) name:@"YBReceiveNotification" object:nil];
    self.isFirstIn = YES;
    [self menuData];
   
    
    [self initCellTitleBeignY];
    [self layoutUI];
    self.isMyBranchViewController = YES;
    
}


- (void)menuData{
    /**  第一个菜单  */
    
    NSArray *first = @[
                       @{@"title": @"扫一扫", @"imageString": @"home_nav_saoyisao"},
                       @{@"title": @"消息", @"imageString": @"home_nav_xiaoxi"},
                       @{@"title": @"党费", @"imageString": @"home_nav_jiaodangfei"},
                       @{@"title": @"常见问题", @"imageString": @"home_nav_qindao"}];
    self.firstTitleAndImages = [NSArray yy_modelArrayWithClass:[MyBranchMenuModel class] json:first];
    
    
    /**  第一个菜单  */
    
    NSArray *three = @[
                       @{@"title": @"党建动态", @"imageString": @"home_djdt"},
                       @{@"title": @"网上党校", @"imageString": @"home_zxxx"},
                       @{@"title": @"组织生活", @"imageString": @"home_zzsh"},
                       @{@"title": @"群团活动", @"imageString": @"home_cyhd"}];
    self.threeTitlesAndImage = [NSArray yy_modelArrayWithClass:[MyBranchMenuModel class] json:three];

}

-(void)initCellTitleBeignY{
    //计算一个16号中文字的高度
    self.itemTextHeight = [@"需" boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : LYPFONT(16)} context:nil].size.height;
    //得出cellTitleLabel起始高度
    self.cellBeginY = ItemHeight * 25 + (ItemHeight * 39 - self.itemTextHeight) / 2;
}


#pragma mark - 数据
- (void)netWorking{
    if (self.isFirstIn) {
        [YJProgressHUD showMessage:@"加载中..." duration:10 view:self.view];
    }
    
    __weak typeof(self) weakSelf = self;
    NSString *IS_SEC = @"0";
    //1:书记请求
    //非1：不是书记请求
    
    if (ZJUSERINFORMODEL.isSecretary) {
        IS_SEC = @"1";
    }
    NSString *START_TIME = [[NSUserDefaults standardUserDefaults] objectForKey:@"START_TIME"] ? [[NSUserDefaults standardUserDefaults] objectForKey:@"START_TIME"] : @"";
    NSDictionary *dict = @{@"ACCOUNTID": ZJUSERINFORMODEL.ORG_ACCOUNT.ID,
                           @"DEPTID": ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.ID ? ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.ID : @"",
                           @"BIG_CLASS": @"",
                           @"SMALL_CLASS":@"",
                           @"IS_SEC": IS_SEC,
                           @"USER_ID3":ZJUSERINFORMODEL.USER_MODEL_JOB.ID ? ZJUSERINFORMODEL.USER_MODEL_JOB.ID : @"",
                           @"USER_ID2":ZJUSERINFORMODEL.USER_MODEL_BOOK.ID? ZJUSERINFORMODEL.USER_MODEL_BOOK.ID : @"",
                           @"START_TIME":START_TIME,
                           @"UNIT_ID":ZJUSERINFORMODEL.DEPT_MODEL_BOOK.ID ? ZJUSERINFORMODEL.DEPT_MODEL_BOOK.ID  :@""};
    [NetWorkingManager postWithUrlString:[NetWorkingURL orgloginactionHomePage] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
           
            NSArray *array1 = [NSArray yy_modelArrayWithClass:[ActivityJoinDetailModel class] json:[object objectForKey:@"ACT_MYJOIN_NOT_OVER"]];
            _ORG_LIFE = [[[object objectForKey:@"SHORTCHUT"] objectForKey:@"ORG_LIFE"] integerValue];
            _STUDY = [[[object objectForKey:@"SHORTCHUT"] objectForKey:@"STUDY"] integerValue];
            _GY = [[[object objectForKey:@"SHORTCHUT"] objectForKey:@"GY"] integerValue];
            _NEWS_TOTAL = [[[object objectForKey:@"SHORTCHUT"] objectForKey:@"NEWS_TOTAL"] integerValue];
//            [weakSelf.threeMenuView setDynamicNumberWithNumber:_NEWS_TOTAL];
//            [weakSelf.threeMenuView setStudyNumberWithNumber:_STUDY];
            [weakSelf.dataSouce removeAllObjects];

            [weakSelf.dataSouce addObjectsFromArray:array1];

            [weakSelf initCellDataModel];
            
        }
        else{
            if (self.isFirstIn) {
                [YJProgressHUD hide];
                self.isFirstIn = NO;
            }
        }
    } Fail:^(id object) {
        if (self.isFirstIn) {
            [YJProgressHUD hide];
            self.isFirstIn = NO;
        }
        
        [YJProgressHUD showMessage:@"加载错误" inView:weakSelf.view];
    }];
    
}


-(void)ybNetWorking_header{
    [self initCellDataModel];
}


-(void)initCellDataModel{
    [YBRequestManager request_partyMemberHomePageListWithAccountId:ZJUSERINFORMODEL.ORG_ACCOUNT.ID partyMemberId:ZJUSERINFORMODEL.ORG_ACCOUNT.PARTY_MEMBER_ID unitId:ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.ID successBlock:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            [self.modelArray removeAllObjects];
            //分不同数组添加数据
            for (NSDictionary * theTypeDic in object[@"HOME_LIST"]) {
                NSString * tempTypeString = theTypeDic[@"KEY"];
                NSArray * tempTypeDataArray = object[tempTypeString];
                if (tempTypeDataArray.count > 0) {
                    if ([tempTypeString isEqualToString:@"PAPER_LIST"]) {
                        for (NSDictionary * dic in tempTypeDataArray) {
                            YBBranchNewsModel * model = [YBBranchNewsModel new];
                            model.ID = [dic objectForKey:@"ID"];
                            model.TYPESTR = @"我的考试";
                            model.TITLE = [dic objectForKey:@"PAPER_NAME"];
                            model.titleString = [NSString stringWithFormat:@"[%@]%@" , model.TYPESTR , model.TITLE];
                            model.typeString = @"5";
                            model.ABSTRACT = [dic objectForKey:@"PAPER_DESC"];
                            model.PUBLISHDATE = [dic objectForKey:@"CREATETIME"];
                            [model calculateTitleHeightAndCellHeightWithBeignY:self.cellBeginY itemTextHeight:self.itemTextHeight];
                            [self.modelArray addObject:model];
                        }
                    }
                    if ([tempTypeString isEqualToString:@"ACTS"]) {
                        for (NSDictionary * dic in tempTypeDataArray) {
                            BranchActivityModel * model = [BranchActivityModel yy_modelWithJSON:dic];
                            if (model) {
                                model.titleString = [NSString stringWithFormat:@"[%@]%@" , model.TYPESTR , model.TITLE];
                                model.typeString = @"1";
                                [model calculateTitleHeightAndCellHeightWithBeignY:self.cellBeginY itemTextHeight:self.itemTextHeight];
                                [self.modelArray addObject:model];
                            }
                        }
                    }
                    if ([tempTypeString isEqualToString:@"REQUIREMENTS"]) {
                        for (NSDictionary * dic in tempTypeDataArray) {
                            BranchRequirementModel * model = [BranchRequirementModel yy_modelWithJSON:dic];
                            if (model) {
                                model.titleString = [NSString stringWithFormat:@"[%@]%@" , model.TYPESTR , model.TITLE];
                                model.typeString = @"2";
                                [model calculateTitleHeightAndCellHeightWithBeignY:self.cellBeginY itemTextHeight:self.itemTextHeight];
                                [self.modelArray addObject:model];
                            }
                        }
                    }
                    if ([tempTypeString isEqualToString:@"NEWS"]) {
                        for (NSDictionary * dic in tempTypeDataArray) {
                            for (NSDictionary * tempDic in dic[@"CONTENT"]) {
                                YBBranchNewsModel * model = [YBBranchNewsModel yy_modelWithJSON:tempDic];
                                model.PUBLISHDATE =tempDic[@"CREATETIME"];
                                model.TYPESTR = dic[@"TYPESTR"];
                                if (model) {
                                    model.titleString = [NSString stringWithFormat:@"[%@]%@" , model.TYPESTR , model.TITLE];
                                    model.typeString = @"4";
                                    [model calculateTitleHeightAndCellHeightWithBeignY:self.cellBeginY itemTextHeight:self.itemTextHeight];
                                    [self.modelArray addObject:model];
                                }
                            }
                        }
                    }
                    if ([tempTypeString isEqualToString:@"NOTPAYS"]) {
                        for (NSDictionary * dic in tempTypeDataArray) {
                            YBBranchNoticeModel * model = [YBBranchNoticeModel yy_modelWithJSON:dic];
                            if (model && [model.NOTPAY_NUM integerValue] > 0) {
                                model.titleString = [NSString stringWithFormat:@"[%@]您已经连续%@个月未交党费" , model.TYPESTR , model.NOTPAY_NUM];
                                model.typeString = @"3";
                                [model calculateTitleHeightAndCellHeightWithBeignY:self.cellBeginY itemTextHeight:self.itemTextHeight];
                                [self.modelArray addObject:model];
                            }
                        }
                    }
                    if ([tempTypeString isEqualToString:@"LOGO_IMG"]) {
                        NSDictionary * logoDic = tempTypeDataArray[0];
                        YBLogoDataModel * model = [YBLogoDataModel yy_modelWithJSON:logoDic];
                        model.typeString = @"0";
                        if (self.modelArray.count == 0) {
                            model.cellHeight = ItemHeight * [model.IOS_HEIGHT integerValue];
                            model.isTop = YES;
                        }
                        else{
                            model.cellHeight = ItemHeight * ([model.IOS_HEIGHT integerValue] + 20);
                            model.isTop = NO;
                        }
                        [self.modelArray addObject:model];
                    }
                }
            }
            
            if (self.isFirstIn) {
                [YJProgressHUD hide];
                self.isFirstIn = NO;
            }
            if ([self.tableView.mj_header isRefreshing]) {
                [self.tableView.mj_header endRefreshing];
            }
            [self.tableView reloadData];
            //加号小红点的操作
            NSString * isSignString = [NSString stringWithFormat:@"%@" , object[@"SIGN"][@"SIGN_STATE"]];
            if ([isSignString isEqualToString:@"已签到"]) {
                [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"YBIsSign"];
                [self reloadSignStatusWithStatus:YES];
                [self.firstMenuView setRedViewHideOrShow:YES];
            }
            else{
                [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"YBIsSign"];
                [self reloadSignStatusWithStatus:NO];
                [self.firstMenuView setRedViewHideOrShow:NO];
            }
        }
        else{
            if (self.isFirstIn) {
                [YJProgressHUD hide];
                self.isFirstIn = NO;
            }
            if ([self.tableView.mj_header isRefreshing]) {
                [self.tableView.mj_header endRefreshing];
            }
            [YJProgressHUD showMessage:[NSString stringWithFormat:@"加载错误,错误码:%@" , model.STATE] inView:self.view];
        }
    } failBlock:^(id object) {
        if (self.isFirstIn) {
            [YJProgressHUD hide];
            self.isFirstIn = NO;
        }
        if ([self.tableView.mj_header isRefreshing]) {
            [self.tableView.mj_header endRefreshing];
        }
        [YJProgressHUD showMessage:@"加载错误" inView:self.view];
    }];
    
    
}




#pragma mark - 界面
- (void)layoutUI{
    [self.view addSubview:self.firstMenuView];
    
    UIView *sepaceView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 10)];
    sepaceView.backgroundColor = [UIColor themeGrayColor];
    [self.view addSubview:sepaceView];
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.threeMenuView.height + 10)];
    headView.backgroundColor = [UIColor whiteColor];
    [headView addSubview:sepaceView];
    
    [headView addSubview:self.threeMenuView];
    self.tableView.tableHeaderView = headView;
    
    
    
    [self.view addSubview:self.tableView];
    
    __weak typeof(self) weakself = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
        [weakself ybNetWorking_header];
    }];

}

#pragma mark - MyBranchFirstTableDelegate
- (void)view:(MyBranchMenuWithFourItemView *)view MyBranchMenuWithFourItem_tapIndex:(NSInteger)index{
    if (view == self.firstMenuView) {
    switch (index) {
        case 0:
        {
    
            QRCodeReaderViewController *reader = [QRCodeReaderViewController new];
            
            reader.push = NO;
            reader.modalPresentationStyle = UIModalPresentationFormSheet;
            [reader setCompletionWithBlock:^(NSString *resultAsString) {
                
            } login:^(BOOL successOrFail) {
                
            } sign:^(NSDictionary *successOrFail) {
                if ([successOrFail[@"STATE"] isEqualToString:@"20000"]) {
                    LPSignSuccessShowView *sign = [[LPSignSuccessShowView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], [UIScreen zj_screenHeight])];
                    NSString *point = successOrFail[@"growUp"][@"point"];
                    sign.data = @[point];
                    if ([point integerValue] != 0) {
                        [[UIApplication sharedApplication].keyWindow addSubview:sign];
                    }
                }
            }];
            [self.navigationController pushViewController:reader animated:YES];
           
           
        }
            break;
        case 1:
        {
            
            /**  消息  */
            LPMessageListViewController *vc = [LPMessageListViewController new];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
        case 2://党费缴纳
        {
            HandPartyMoneyViewController *vc = [HandPartyMoneyViewController new];
            NSString *urlString = [NSString stringWithFormat:@"%@/WeChat/views/index_hd.html#/onePayment_shibei/%@/%@",HTMLURL,ZJUSERINFORMODEL.ORG_ACCOUNT.ID,ZJUSERINFORMODEL.ORG_ACCOUNT.PARTY_MEMBER_ID];
            vc.titleString = @"党费缴纳";
            vc.urlString = urlString;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
        case 3://常见问题
        {
            LPNineTwentyNormalQuestionViewController *vc = [LPNineTwentyNormalQuestionViewController new];
            [self.navigationController pushViewController:vc animated:YES];
            
        }
            break;
            
            
        default:
            break;
    }
    }else if (view == self.threeMenuView){
        switch (index) {
            case 0://党建动态
            {
                LPNineTwentySixPartyDynamicViewController *vc = [LPNineTwentySixPartyDynamicViewController new];

                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
            case 1:// 网上党校
            {
                LPNineTenwtySixOnlineSchoolViewController *vc = [LPNineTenwtySixOnlineSchoolViewController new];
                [[ToolClass getMainNavigationController] pushViewController:vc animated:YES];
                
            }
                break;
                
            case 2://组织生活
            {
                //组织生活
                OnlineOrganizationalLifeViewController *vc = [OnlineOrganizationalLifeViewController new];
                [self.navigationController pushViewController:vc animated:YES];
        
            }
                break;
                
            case 3://参与活动
            {
                
                ZJParticipateActivitiesViewController *vc = [ZJParticipateActivitiesViewController new];
                [self.navigationController pushViewController:vc animated:YES];
                
//                LPNewCommonActListViewController *vc = [LPNewCommonActListViewController new];
//                vc.headLine = @"群团活动";
//                [self.navigationController pushViewController:vc animated:YES];
                
            }
                break;
            default:
                break;
        }
    }
}


/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
    
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    BranchNormalModel * model = self.modelArray[indexPath.row];
    return model.cellHeight;
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    BranchNormalModel * model = self.modelArray[indexPath.row];
    if ([model.typeString integerValue] == 0) {
        YBHomeLogoTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:HOMELOGOCELL forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = (YBLogoDataModel *)model;
        return cell;
    }
    else if ([model.typeString integerValue] == 1) {
        OrganizationalLifeTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ORGCELL forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.titleBeginY = self.cellBeginY;
        cell.model = model;
        [cell.optionButton addTarget:self action:@selector(option_askForLeaveButton:) forControlEvents:UIControlEventTouchUpInside];
        cell.optionButton.tag = 2000 + indexPath.row;
        return cell;
    }
    else if([model.typeString integerValue] == 2){
        NeedClaimTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NEEDCELL forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.optionButton addTarget:self action:@selector(option_ensureCompleteButton:) forControlEvents:UIControlEventTouchUpInside];
        cell.optionButton.tag = 2000 + indexPath.row;
        cell.titleBeginY = self.cellBeginY;
        cell.model = model;
        return cell;
    }
    else if([model.typeString integerValue] == 3){
        YBDuesPayTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:DUESCELL forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.titleBeginY = self.cellBeginY;
        cell.model = model;
        return cell;
    }
    else{ //我的考试  和   党建动态
        ConstructionDynamicTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CONSTRCELL forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.titleBeginY = self.cellBeginY;
        cell.model = model;
        return cell;
    }
}




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BranchNormalModel * model = self.modelArray[indexPath.row];
    NSInteger cellType = [model.typeString integerValue];
    switch (cellType) {
        case 0:{
            YBLogoDataModel * tempModel = (YBLogoDataModel *)model;
            ZJWebViewController *vc = [ZJWebViewController new];
            vc.titleString = tempModel.TITLE;
            vc.urlString = [NSString stringWithFormat:@"%@" , tempModel.URL];
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        case 1:{
             BranchActivityModel * tempModel = (BranchActivityModel *)model;
            if ([tempModel.BIG_CLASS integerValue] == 1 && [tempModel.ONLINE integerValue] == 1) {
                /**  跳转组织生活详情  */
                OnlineOLDetailViewController *vc = [OnlineOLDetailViewController new];
                OnlineOrganizationalLifeModel *model1= [OnlineOrganizationalLifeModel new];
                model1.ID = tempModel.ACT_ID;
                model1.NAME = tempModel.TITLE;
                vc.model = model1;
                [self.navigationController pushViewController:vc animated:YES];

            }else{
                /**  跳转活动详情  */
                LPActivityDetailViewController *vc = [LPActivityDetailViewController new];
                ActivityJoinDetailModel *model1= [ActivityJoinDetailModel new];
                model1.ID = tempModel.ACT_ID;
                model1.NAME = tempModel.TITLE;
                model1.START_TIME = tempModel.START_TIME;
                model1.END_TIME = tempModel.END_TIME;
                model1.ADDRESS = tempModel.ACT_ADDRESS;
                model1.IS_LEAVE = tempModel.LEAVE_STATUS;
                vc.model = model1;
                [self.navigationController pushViewController:vc animated:YES];

            }
            
            break;
        }
        case 2:{
            self.currentSelectRequireModel = (BranchRequirementModel *)model;
            ZJShareClaimViewController *vc = [ZJShareClaimViewController new];
            ShareOpenRequestModel *model1 = [ShareOpenRequestModel new];
            model1.ID = self.currentSelectRequireModel.REQ_ID;
            vc.model = model1;
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        case 3:{
            //党费交纳
            HandPartyMoneyViewController *vc = [HandPartyMoneyViewController new];
            NSString *urlString = [NSString stringWithFormat:@"%@/WeChat/views/index_hd.html#/onePayment_shibei/%@/%@",HTMLURL,ZJUSERINFORMODEL.ORG_ACCOUNT.ID,ZJUSERINFORMODEL.ORG_ACCOUNT.PARTY_MEMBER_ID];
            vc.titleString = @"党费缴纳";
            vc.urlString = urlString;
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        case 4:{
            LPDCDetailModel *newModel = [LPDCDetailModel new];
            YBBranchNewsModel * tempModel = (YBBranchNewsModel *)model;
            newModel.ID = tempModel.NEWS_ID;
            newModel.TITLE = tempModel.TITLE;
            newModel.URL = tempModel.URL;
            LPWKHasCommentAndHeadLineViewController *vc = [LPWKHasCommentAndHeadLineViewController new];
            vc.model = newModel;

            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        case 5:{
            YBBranchNewsModel * tempModel = (YBBranchNewsModel *)model;
            ZJQuestionWebViewController *vc = [ZJQuestionWebViewController new];
            vc.titleString = tempModel.TITLE;
            vc.ID = tempModel.ID;
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }

        default:
            break;
    }
}


///获取接收人id
- (void)netWorking_ID{
    
    __weak typeof(self) weakSelf = self;
    NSDictionary *dict = @{@"ID":self.currentSelectRequireModel.REQ_ID};
    [NetWorkingManager postWithUrlString:[NetWorkingURL informactionSelectShareInfo] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            [weakSelf netWorking_compClaim:[object objectForKey:@"INFO_MEG"]];
        }else{
            [YJProgressHUD showSuccess:@"失败" inview:weakSelf.view];
        }
    } Fail:^(id object) {
        [YJProgressHUD showSuccess:@"失败" inview:weakSelf.view];
    }];
}

///完成认领
/**
 formUsetDict:
 ID
	ISCOMPLETED
	SHARE_UNITID
	TO_USERID
	TO_UNITID
	CREATE_TIME
 
 */
- (void)netWorking_compClaim:(NSDictionary *)formUsetDict{
    /*REL_ID	项目ID
     REL_TYPE	类型   qa:QA库   jf:积分管理   pd:党员审核    resource:资源管理   act:活动  need 发布需求
     SOURCE_USERID	接受人ID
     SOURCE_UNITID	接受人机构ID
     CONTENT	内容
     FROM_USERID	发送人ID
     FROM_UNITID	发送人机构ID */
    __weak typeof(self) weakSelf = self;
    NSDictionary *dict = @{@"REL_ID":self.currentSelectRequireModel.REL_ID,
                           @"REL_TYPE":@"need",
                           @"TO_USERID":[formUsetDict objectForKey:@"SOURCE_USERID"],
                           @"TO_UNITID":[formUsetDict objectForKey:@"SOURCE_UNITID"],
                           @"CONTENT":self.currentSelectRequireModel.CONTENT,
                           @"FROM_USERID":[ZJUserInfoModel sharedUserInfo].CURRENT_USER_MODEL.ID,
                           @"FROM_UNITID":[ZJUserInfoModel sharedUserInfo].CURRENT_DEPT_MODEL.ID};
    [NetWorkingManager postWithUrlString:[NetWorkingURL informactionSetMegSingle] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {//完成认领
            [MobClick event:[UMMStatisticsString finishReq]];
            [YJProgressHUD showSuccess:@"确认成功" inview:weakSelf.view];
            [self.tableView.mj_header beginRefreshing];
            
        }else{
            [YJProgressHUD showSuccess:@"失败" inview:weakSelf.view];
        }
    } Fail:^(id object) {
        [YJProgressHUD showSuccess:@"失败" inview:weakSelf.view];
    }];
}
//修改状态
- (void)changeIDSTATE{
    [YJProgressHUD showMessage:@"确认中..." duration:10 view:self.view];
    __weak typeof(self) weakSelf = self;
    NSDictionary *dict = @{@"ID":self.currentSelectRequireModel.REQ_ID, @"REL_ID": self.currentSelectRequireModel.REL_ID};
    [NetWorkingManager postWithUrlString:[NetWorkingURL pbactionRequirementfinishRl] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {//完成认领
            [weakSelf netWorking_ID];
        }else{
            [YJProgressHUD showSuccess:@"失败" inview:weakSelf.view];
        }
    } Fail:^(id object) {
        [YJProgressHUD showSuccess:@"失败" inview:weakSelf.view];
    }];
}



#pragma mark 请假按钮
-(void)option_askForLeaveButton:(UIButton *)sender{
    self.currentSelectModel = (BranchActivityModel *)(self.modelArray[sender.tag - 2000]);
    if ([self.currentSelectModel.STATUS integerValue] == 0) {
        [YJProgressHUD showMessage:@"报名中..." duration:10 view:self.view];
        NSDictionary *paramer = @{@"AI_ID": self.currentSelectModel.ACT_ID,
                                  @"USER_ID": ZJUSERINFORMODEL.CURRENT_USER_MODEL.ID,
                                  @"USER_NAME": ZJUSERINFORMODEL.ORG_ACCOUNT.USER_NAME};
        __weak typeof(self) weakSelf = self;
        [NetWorkingManager postWithUrlString:[NetWorkingURL getactactionMobileEnroll] Parameter:paramer Success:^(id object) {
            if ([object[@"CODE"] isEqualToString:@"20000"]) {
                [MobClick event:[UMMStatisticsString registerAct]];
                [YJProgressHUD showMsgWithoutView:@"报名成功"];
                [weakSelf initCellDataModel];
                
            }else if ([object[@"CODE"] isEqualToString:@"80001"]) {
                [YJProgressHUD showMsgWithoutView:@"重复报名"];
            }else if ([object[@"CODE"] isEqualToString:@"80002"]) {
                [YJProgressHUD showMsgWithoutView:@"活动结束"];
            }else if ([object[@"CODE"] isEqualToString:@"80003"]) {
                [YJProgressHUD showMsgWithoutView:@"人数达上限"];
            }else if ([object[@"CODE"] isEqualToString:@"80004"]) {
                [YJProgressHUD showMsgWithoutView:@"无此活动或已被删除!"];
            }else{
                [YJProgressHUD showMsgWithoutView:@"报名失败"];
            }
            
        } Fail:^(id object) {
            [YJProgressHUD showMsgWithoutView:@"报名失败"];
        }];
        
    }
    else{
        LPCostomHeadLineAndTextViewAndTiShiView *tishi = [[LPCostomHeadLineAndTextViewAndTiShiView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, [UIScreen zj_screenHeight]) headLine:@"请输入原因"];
        tishi.delegate = self;
        self.alertView = [[JCAlertView alloc] initWithCustomView:tishi dismissWhenTouchedBackground:YES];
        [self.alertView show];
    }
}

#pragma mark 确认完成按钮
-(void)option_ensureCompleteButton:(UIButton *)sender{
    self.currentSelectRequireModel = (BranchRequirementModel *)(self.modelArray[sender.tag - 2000]);
    [self changeIDSTATE];
}

#pragma mark - LPCostomHeadLineAndTextViewAndTiShiDelegate
- (void)LPCostomHeadLineAndTextViewAndTiShiWithCancle:(BOOL)cancle{
    [self.alertView dismissWithCompletion:nil];
}

- (void)LPCostomHeadLineAndTextViewAndTiShiWithString:(NSString *)string{
    /**   这里是请假  */
    [YJProgressHUD show:@"请假中..." inView:self.view mode:YJProgressModeLoading];
    NSDictionary *paramer = @{@"AI_ID": self.currentSelectModel.ACT_ID,
                              @"FROM_USERID": ZJUSERINFORMODEL.CURRENT_USER_MODEL.ID,
                              @"FROM_USERNAME": ZJUSERINFORMODEL.ORG_ACCOUNT.USER_NAME,
                              @"FROM_UNITID": ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.ID,
                              @"PMID": ZJUSERINFORMODEL.ORG_ACCOUNT.PARTY_MEMBER_ID,
                              @"CONTENT": string,
                              @"TO_UNITID": self.currentSelectModel.ACT_UNITID,
                              @"FROM_UNITNAME": ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.DEPT_NAME,
                              @"ACT_NAME": self.currentSelectModel.TITLE};
    
    [NetWorkingManager postWithUrlString:[NetWorkingURL getactmactionActLeave] Parameter:paramer Success:^(id object) {
        if ([object[@"STATE"] isEqualToString:@"20000"]) {
            [YJProgressHUD showMsgWithoutView:@"请假成功"];
            [self initCellDataModel];
        }else{
            [YJProgressHUD showMsgWithoutView:@"请假失败"];
        }
        
    } Fail:^(id object) {
        [YJProgressHUD showMsgWithoutView:@"请假失败"];
    }];
    
    [self.alertView dismissWithCompletion:nil];
    
}


#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.firstMenuView.bottom, self.view.bounds.size.width, self.view.bounds.size.height  - self.firstMenuView.bottom) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [_tableView registerClass:[HomeMenuTableViewCell class] forCellReuseIdentifier:MENUCELL];
        [_tableView registerClass:[MyBranchThreeTableViewCell class] forCellReuseIdentifier:ACTLISTCELL];
        [_tableView registerClass:[OrganizationalLifeTableViewCell class] forCellReuseIdentifier:ORGCELL];
        [_tableView registerClass:[NeedClaimTableViewCell class] forCellReuseIdentifier:NEEDCELL];
        [_tableView registerClass:[YBDuesPayTableViewCell class] forCellReuseIdentifier:DUESCELL];
        [_tableView registerClass:[ConstructionDynamicTableViewCell class] forCellReuseIdentifier:CONSTRCELL];
        [_tableView registerClass:[YBHomeLogoTableViewCell class] forCellReuseIdentifier:HOMELOGOCELL];
        _tableView.backgroundColor = [UIColor themeGrayColor];
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}


- (MyBranchMenuWithFourItemView *)firstMenuView{
    if (!_firstMenuView) {
        _firstMenuView = [[MyBranchMenuWithFourItemView alloc] initWithFrame:CGRectMake(0, LP_TopHeight, self.view.width, 80 * [UIScreen zj_screenHScale])];
        _firstMenuView.messageTextColor = [UIColor themeColor];
        _firstMenuView.messageBgColor = [UIColor whiteColor];
        _firstMenuView.data = self.firstTitleAndImages;
        _firstMenuView.delegate = self;
    }
    return _firstMenuView;
}

- (MyBranchMenuWithFourItemView *)threeMenuView{
    if (!_threeMenuView) {
        NSInteger zheng = self.threeTitlesAndImage.count / 4;
        NSInteger yu = self.threeTitlesAndImage.count % 4;
        if (yu > 0) {
            zheng ++;
        }
        _threeMenuView = [[MyBranchMenuWithFourItemView alloc] initWithFrame:CGRectMake(0, 10 , self.view.width, 80 * zheng * [UIScreen zj_screenHScale])];
        _threeMenuView.backgroundColor = [UIColor clearColor];
        _threeMenuView.messageBgColor = [UIColor themeColor];
        _threeMenuView.messageTextColor = [UIColor whiteColor];
        _threeMenuView.textColor = [UIColor zj_colorWithHexString:@"626262"];
        _threeMenuView.data = self.threeTitlesAndImage;
        _threeMenuView.delegate = self;
    }
    return _threeMenuView;
}


- (ZJRefurbish *)refurbish{
    if (!_refurbish ) {
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}

- (void)cancleHigh:(UIButton *)sender{
    sender.highlighted = NO;
}

- (NSMutableArray *)dataSouce{
    if (!_dataSouce ) {
        _dataSouce = [NSMutableArray array];
    }
    return _dataSouce;
}

-(NSMutableArray *)cellHeightArray
{
    if (!_cellHeightArray) {
        _cellHeightArray = [NSMutableArray array];
    }
    return _cellHeightArray;
}


-(NSMutableArray *)modelArray
{
    if (!_modelArray) {
        _modelArray = [NSMutableArray array];
    }
    return _modelArray;
}


-(NSMutableArray *)titleHeightArray
{
    if (!_titleHeightArray) {
        _titleHeightArray = [NSMutableArray array];
    }
    return _titleHeightArray;
}



@end
